//
//  YYMusicChildMusicerController.swift
//  STMusic
//
//  Created by stoneobs on 2017/6/12.
//  Copyright © 2017年 stoneobs. All rights reserved.
//

import UIKit

class YYMusicChildMusicerController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
    var page = 1;
    
    var dataSouce:Array<YYMusicPeopleModel> = [];
    
    var collectionView:UICollectionView?;
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = FlatOrange();
        
        //collectionview
        let flowLaout = UICollectionViewFlowLayout.init();
        flowLaout.minimumLineSpacing = 0;
        flowLaout.minimumInteritemSpacing = 0;
        flowLaout.scrollDirection = .vertical;
        flowLaout.sectionInset = .init(top: 0, left: 0, bottom: 0, right: 0);
        self.collectionView = UICollectionView.init(frame: CGRect.init(x: 0, y: 0, width: SCREEN_WIDTH, height: YYMusicArticalViewController.scrollViewHeight), collectionViewLayout: flowLaout);
        self.collectionView?.showsVerticalScrollIndicator = false;
        self.collectionView?.delegate = self;
        self.collectionView?.dataSource = self;
        self.collectionView?.alwaysBounceVertical = false;
        self.collectionView?.alwaysBounceHorizontal = false;
        self.collectionView?.register(YYMusicPeopleCollectionViewCell.self, forCellWithReuseIdentifier: "item")
        self.collectionView?.backgroundColor = UIColor.white.withAlphaComponent(0.7);
        self.view.backgroundColor = .clear;
        self.view.addSubview(self.collectionView!);

        
        self.loadNetworkRequest();
        
        //
        weak var weakSelf = self;
        let header:MJRefreshGifHeader = MJRefreshGifHeader.init(refreshingBlock: {
            weakSelf?.page = 1;
            weakSelf?.dataSouce = [];
            weakSelf?.loadNetworkRequest();
            
        });
        header.lastUpdatedTimeLabel.textColor = FirstTextColor;
        header.stateLabel.textColor = FirstTextColor;
        self.collectionView?.mj_header = header;
      
    }
    // MARK: - UICollectionViewDataSource
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1;
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.dataSouce.count;
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let witdh = SCREEN_WIDTH / 3;
        return CGSize.init(width: witdh, height: 90);
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell:YYMusicPeopleCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "item", for: indexPath) as! YYMusicPeopleCollectionViewCell;
        cell.model = self.dataSouce[indexPath.row];
        return cell;
        
    }
        // MARK: - UICollectionViewDelegate
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let model = self.dataSouce[indexPath.row];
        let vc:YYMusicPeopleDetailController =  YYMusicPeopleDetailController();
        vc.model = model;
        self.navigationController?.pushViewController(vc, animated: true);
    }
        
    func loadNetworkRequest() -> Void {
  
        let str = "http://wawa.fm:9090/wawa/api.php/Musician/list_/type/0/r/18/page/\(self.page)"
        YYYuYinNetWorkManger.defultManger.getNetwork(urlString: str, dic: [:], isJson: true, successHandle: { (json) in
            
            print("json = \(json)");
            
            var isUpdate = false;
            if let array:Array<Dictionary<String,Any>> = json as? Array{
                
                for objectdic in array{
                    
                    let model:YYMusicPeopleModel = YYMusicPeopleModel.mj_object(withKeyValues: objectdic);
                    isUpdate = true;
                    self.dataSouce.append(model);
                    
                }
                
            }
            
            if self.dataSouce.count > 9 && (self.collectionView?.mj_footer == nil ){
                weak var weakSelf = self;
                self.collectionView?.mj_footer = MJRefreshAutoGifFooter.init(refreshingBlock: {
                    weakSelf?.page  = self.page + 1;
                    weakSelf?.loadNetworkRequest();
                });
            }
            
            
            
            self.collectionView?.mj_header?.endRefreshing();
            self.collectionView?.mj_footer?.endRefreshing();
            
            if isUpdate == false {
                self.collectionView?.mj_footer?.endRefreshingWithNoMoreData();
            }
            self.collectionView?.reloadData();
            
            
        }) { (str) in
            self.collectionView?.mj_header.endRefreshing();
            self.collectionView?.mj_footer?.endRefreshing();
        };
    }
    




}
